package com.hysoft.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class FileRenameUtil {
    /**
     * access文件导入oracle出错时，要求把原有文件加上_ERR字样之后，原样输出到之前的目录中
     * @param path
     * @return
     */
    public static String renameFile(String path){
        //旧路径
        String oldpath = path;
        //文件前缀
        String prefix = "";
        //文件后缀
        String suffix = "";
        //新路径
        String newpath = "";
        List<String> list = new ArrayList<>();
        for (String retval: oldpath.split("\\.")){
            list.add(retval);
            System.out.println(retval);
        }
        prefix = list.get(0);
        suffix = list.get(1);
        try {
            File src = new File(oldpath);
            newpath = prefix+"_ERR."+suffix;
            //改名
            File des = new File(newpath);
            if (!src.renameTo(des)) {
                System.out.println("Failed to renameTo file");
                LogUtil.inserCorrectLog("In","Failed to renameTo file","error.txt");
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return newpath;
    }
}
